﻿using DataInspector;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace DataInspectorDemo
{
    class Program
    {
        static void Main(string[] args)
        {
            FindString("just");
            FindInt32((Int32)44100);
            ReadInt32(24);
            Read(12, 4);
            SaveFile();
        }

        static void FindInt32(Int32 input)
        {
            System.Diagnostics.Debug.WriteLine("FindInt32");

            string filename = "Sine_wave_440.wav";

            using (DataInspector.DataInspector inspector = new DataInspector.DataInspector(filename))
            {
                byte[] data = BitConverter.GetBytes(input);

                long position = inspector.Find(data, (pos, tot) => {

                    System.Diagnostics.Debug.WriteLine(String.Format("pos: {0}, tot: {1}", pos, tot));

                });

                System.Diagnostics.Debug.WriteLine(String.Format("index found: {0}", position));
            }
        }

        static void FindString(string input)
        {
            System.Diagnostics.Debug.WriteLine("FindString");

            string filename = "demo.txt";

            using (DataInspector.DataInspector inspector = new DataInspector.DataInspector(filename))
            {
                byte[] data = System.Text.Encoding.UTF8.GetBytes(input);

                long position = inspector.Find(data, (pos, tot) => {

                    System.Diagnostics.Debug.WriteLine(String.Format("pos: {0}, tot: {1}", pos, tot));

                });

                System.Diagnostics.Debug.WriteLine(String.Format("index found: {0}", position));
            }
        }

        static void ReadInt32(int offset)
        {
            System.Diagnostics.Debug.WriteLine("ReadInt32");

            string filename = "Sine_wave_440.wav";

            using (DataInspector.DataInspector inspector = new DataInspector.DataInspector(filename))
            {
                inspector.Stream.Position = offset;
                
                int result = inspector.Read<Int32>();

                System.Diagnostics.Debug.WriteLine(String.Format("data: {0}", result));                
            }
        }

        static void Read(int offset, int len)
        {
            System.Diagnostics.Debug.WriteLine("Read");

            string filename = "Sine_wave_440.wav";

            using (DataInspector.DataInspector inspector = new DataInspector.DataInspector(filename))
            {
                inspector.Stream.Position = offset;

                byte[] data = inspector.Read(len);

                string result = System.Text.Encoding.UTF8.GetString(data);

                System.Diagnostics.Debug.WriteLine(String.Format("data: {0}", result));
            }
        }

        static void SaveFile()
        {
            System.Diagnostics.Debug.WriteLine("SaveFile");

            string filename = "demo.txt";

            MemoryStream ms = new MemoryStream();

            using (FileStream fs = new FileStream(filename, FileMode.Open))
            {
                DIUtils.CopyStream(fs, ms, fs.Length, (pos, tot) =>
                {
                    System.Diagnostics.Debug.WriteLine(String.Format("pos: {0}, tot: {1}", pos, tot));
                });

                ms.Position = 0;
            }

            using (DataInspector.DataInspector inspector = new DataInspector.DataInspector(ms))
            {
                string str2find = "demo";

                byte[] data = System.Text.Encoding.UTF8.GetBytes(str2find);

                long index = inspector.Find(data, null);

                inspector.Stream.Position = index;

                byte[] data2Insert = System.Text.Encoding.UTF8.GetBytes("modified ");

                inspector.Insert(data2Insert, null);

                string output = @"D:\Temp\demo_mod.txt";

                DIUtils.SaveToFile(inspector.Stream, output, (pos, tot) =>
                {
                    System.Diagnostics.Debug.WriteLine(String.Format("pos: {0}, tot: {1}", pos, tot));
                });
            }
        }
    }
}
